package  
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	/**
	 * ...
	 * @author Anders Figenschow selvfølgelig
	 */
	public class SoundPlayer extends Sprite
	{
		private var sound:Sound;
		private var _main:Main;
		private var _vol:Number = 1;
		private var channel:SoundChannel;
		private var isPlaying:Boolean = false;
		private var lastSoundPosition:Number;
		
		public function SoundPlayer(main:Main) 
		{
			_main = main;
			initSound();
			
			
		}
		
		public function getSound():Sound {
			return sound;
		}
		
		public function playMusic(_url:String):void {
			sound.load(new URLRequest(_url));	
			
		}
		private function initSound():void 
		{
			//sound = new Sound();
			//sound.addEventListener(Event.COMPLETE, loadComplete);
			//sound.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			//sound.addEventListener(Event.ID3, id3Handler);
			//sound.addEventListener(ProgressEvent.PROGRESS, onSoundLoadProgress);
			
		}
		public function play():void {
			sound.play();
		}
		public function resume():void {
			if (!sound) {
				initSound();
			}
			isPlaying = true;
			channel = sound.play(lastSoundPosition);
			channel.soundTransform = new SoundTransform(_vol);			
		}
		public function pause():void {
			isPlaying = false;
			lastSoundPosition = channel.position;
			channel.stop();
		}
		public function stop():void {
			isPlaying = false;
			lastSoundPosition = 0;
			
			// TODO make the stop function return cursor to beginning
			
			channel.soundTransform = new SoundTransform(_vol);
			channel.stop();
			//sound.removeEventListener(Event.COMPLETE, loadComplete);
			//sound.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			//sound.removeEventListener(Event.ID3, id3Handler);
			//sound.removeEventListener(ProgressEvent.PROGRESS, onSoundLoadProgress);
			
		}
		
	}

}


